package com.mbengue.corp.apr.ra.util;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mbengue.corp.apr.ra.beans.LoggingBean;

public class LogginFilter implements javax.servlet.Filter {
  /*
   * (non-Javadoc)
   * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
   */
  @Override
  public void doFilter(ServletRequest aRequest, ServletResponse aResponse, FilterChain aChain) throws IOException,
      ServletException {
    HttpServletRequest req = (HttpServletRequest) aRequest;
    HttpServletResponse res = (HttpServletResponse) aResponse;
    //
    LoggingBean loggingManager = (LoggingBean) req.getSession().getAttribute("logging");
    if (loggingManager == null || !loggingManager.getLoggedIn()) {
      res.sendRedirect(req.getContextPath() + "/login.xhtml");
    } else {
      aChain.doFilter(aRequest, aResponse);
    }
  }

  /*
   * (non-Javadoc)
   * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
   */
  @Override
  public void init(FilterConfig aFilterConfig) throws ServletException {
    // TODO Auto-generated method stub
  }

  /*
   * (non-Javadoc)
   * @see javax.servlet.Filter#destroy()
   */
  @Override
  public void destroy() {
    // TODO Auto-generated method stub
  }
}
